package com.topboya.linker.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.SelectVistorItem;

import java.util.ArrayList;
import java.util.List;

//自己写的adapter在返回的时候数据老有问题，后面在研究一下
public class SeeVistorRightAdapter extends RecyclerView.Adapter<SeeVistorRightAdapter.ListItemViewHolder> {

    private static final String TAG = "SelecVistorRightAdapter:";
    private List<List<SelectVistorItem>> departmentLists;
//    切换左边的adapter的position
    private int positon;
//    记录被选中的个数
    private int count;

    private boolean isCreateTask;

    public SeeVistorRightAdapter() { }

   public void SetisCreateTask(boolean isCreateTask){

       this.isCreateTask = isCreateTask;

   }
    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_vistor_item_right, viewGroup, false);
        return new ListItemViewHolder(itemView);

    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int position) {

        holder.tv_visit_name_right.setText(departmentLists.get(this.positon).get(position).getCusName());
//        被选中计数器就加1
//        if (ischecked) count++;

            holder.ck_select.setVisibility(View.GONE);

        //点击事件监听
        clickListenter(holder, position);

    }


    private void clickListenter(ListItemViewHolder holder, int positon) {
        holder.itemView.setOnClickListener(v -> {

            if (null !=itemClickListener){
                itemClickListener.onClick(holder.itemView,positon,departmentLists.get(this.positon).get(positon));
            }
        });
    }

    @Override
    public int getItemCount() {

        return departmentLists ==null ? 0 :departmentLists.get(positon) == null ? 0 : departmentLists.get(positon).size();
    }

    public void updateData(List<List<SelectVistorItem>> departmentLists, int positon) {
        this.departmentLists = departmentLists;
        this.positon = positon;
        notifyDataSetChanged();

    }
//     获取被选中的拜访对象集合
    public List<SelectVistorItem> getSelectDepartmentLists() {
       List<SelectVistorItem> departmentLists = new ArrayList<>();

       for (int i =0; i < this.departmentLists.size();i++){

           for (int j =0;j < this.departmentLists.get(i).size();j++){

               SelectVistorItem department = this.departmentLists.get(i).get(j);
               if (department.isChecked()){

                   departmentLists.add(department);
               }
           }
       }
        return departmentLists;
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_visit_name_right;
        CheckBox ck_select;

        ListItemViewHolder(View view) {

            super(view);
            this.tv_visit_name_right = view.findViewById(R.id.tv_visit_name_right);
            this.ck_select = view.findViewById(R.id.ck_select);

        }

    }


    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SeeVistorRightAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position,SelectVistorItem selectVistorItem);
    }

    private OnItemChildClickListener itemChildClickListener;

    public void setOnitemChildCkickListener(OnItemChildClickListener itemChildClickListener){

        this.itemChildClickListener = itemChildClickListener;
    }
    public interface  OnItemChildClickListener{

        void onclick(View view, int position, int count);
    }

}
